package print;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Button;
import android.widget.Toast;

import com.hxb.deviceservice.aidl.IDeviceService;
import com.hxb.deviceservice.aidl.printer.IPrinter;
import com.hxb.deviceservice.aidl.printer.OnPrintListener;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/9/4.
 */

public class PrintUtils {
    private Button mPrint;
    private IPrinter iPrinter;
    private IBinder printer;
    private PrintUtils pu = null;
    private OnPrintListener onPrintListener;

    private PrintUtils() {

    }

    private PrintUtils(final Context context, final OnStateListener onStateListener) {
        ServiceConnection sc = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IDeviceService deviceService = IDeviceService.Stub.asInterface(service);
                try {
                    printer = deviceService.getPrinter();
                    iPrinter = IPrinter.Stub.asInterface(printer);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                onStateListener.onServiceDisConnection();
            }
        };


        onPrintListener = new OnPrintListener.Stub() {

            @Override
            public void onFinish() throws RemoteException {
                onStateListener.onPrintSuccess();
            }

            @Override
            public void onError(int i) throws RemoteException {
                onStateListener.onPrintFail();
            }
        };

        Intent intent = new Intent();
        intent.setPackage("com.hxb.deviceservice");//兼容Android 5.0
        intent.setAction("com.hxb.device_service");
        boolean b = context.bindService(intent, sc, Service.BIND_AUTO_CREATE);
    }

    public PrintUtils getInstance(Context context, OnStateListener onPrintStateListener) {

        if (pu == null) {
            pu = new PrintUtils(context, onPrintStateListener);
        }
        return pu;
    }

    /**
     * 打印字符
     *
     * @param data 需要打印的byte数组
     * @throws RemoteException
     */
    public void printData(String data,int font,int align) throws RemoteException {

        Bundle bundle = new Bundle();
        bundle.putInt("font", font);
        bundle.putInt("align", align);
        iPrinter.addText(bundle, data);
        iPrinter.feedLine(5);

        iPrinter.startPrint(onPrintListener);

    }


    /**
     * 打印二维码
     *
     * @param QrCode 二维码数据
     * @throws RemoteException
     */
    public void printQrCode(String QrCode) throws RemoteException {
        Bundle bundle = new Bundle();
        bundle.putInt("expectedHeight", 256);
        bundle.putInt("align", 1);
        iPrinter.addQrCode(bundle, QrCode);
        iPrinter.feedLine(4);
        iPrinter.startPrint(onPrintListener);
    }

    /**
     * 打印条码
     *
     * @param barcode 需打印条码的数据
     * @throws RemoteException
     */
    public void printBarCode(String barcode) throws RemoteException {
        Bundle bundle = new Bundle();
        bundle.putInt("codeWidth", 7);
        bundle.putInt("codeHeight", 32);
        bundle.putInt("align", 1);
        iPrinter.addBarCode(bundle, barcode);
        iPrinter.feedLine(4);
        iPrinter.startPrint(onPrintListener);
    }

    /**
     * 打印图片
     * @param context 上下文
     * @param imgName 图片名称包括后缀 要求 单色 bmp 图片，位图深度 1
     * @param imgData 图片数据
     * @throws RemoteException
     * @throws IOException
     */
    public void printImg(Context context,String imgName,byte[] imgData) throws RemoteException, IOException {
        InputStream ins = context.getAssets().open(imgName);
        Bitmap bmp = BitmapFactory.decodeStream(ins);
        byte[] image = Bitmap2Bytes(bmp);
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        width = 0 == width % 8 ? width : width + (8 - width % 8);

        Bundle imageBun = new Bundle();
        imageBun.putInt("offset", 0);
        imageBun.putInt("width", width);
        imageBun.putInt("height", height);
        iPrinter.addImage(imageBun, image);
        iPrinter.feedLine(3);
        iPrinter.startPrint(onPrintListener);
    }
    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }


}
